package org.vehicle.iface;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.vehicle.entity.AlertInfo;
import org.vehicle.entity.CheckRecord;
import org.vehicle.entity.ManagerInfo;
import org.vehicle.entity.OrganizationInfo;
import org.vehicle.entity.Organization_vehicle;
import org.vehicle.entity.PersonInfo;
import org.vehicle.entity.TripInfo;
import org.vehicle.entity.VehicleInfo;
import org.vehicle.service.AlertInfoService;
import org.vehicle.service.CheckRecordService;
import org.vehicle.service.OrganizationInfoService;
import org.vehicle.service.Organization_vehicleService;
import org.vehicle.service.PersonInfoService;
import org.vehicle.service.TripService;
import org.vehicle.service.VehicleInfoService;
import org.vehicle.service.redis.RedisService;
import org.vehicle.util.CommonUtil;
import org.vehicle.util.HttpClientUtil;
import org.vehicle.util.JsonUtils;
import org.vehicle.vo.CheckCarInfo;
import org.vehicle.vo.DeviceVehicleVO;



@Controller
@RequestMapping("interface/deviceVehicle")
public class DeviceVehicleController {
	private static Logger logger = LogManager.getLogger(DeviceVehicleController.class.getName());
	@Autowired
	private OrganizationInfoService organizationInfoService;
	@Autowired
	private VehicleInfoService vehicleInfoService;
	@Autowired
	private PersonInfoService personInfoService;
	@Autowired
	private AlertInfoService alertInfoService;
	@Autowired
	private Organization_vehicleService organization_vehicleService;
	@Autowired
	private TripService tripService;
	@Autowired
	private CheckRecordService checkRecordService;
	@Autowired
	private RedisService redisService;
	/**
	 * 获取车辆信息
	 * @param deviceVehicleVO
	 * @return
	 */
	@RequestMapping("getVehicleInfo")
	@ResponseBody
	public ResponseEntity<Map<String, Object>> getVehicleInfo(@RequestBody DeviceVehicleVO deviceVehicleVO){
		Map<String, Object> retMap=new HashMap<String, Object>();
		try{
			VehicleInfo vehicleInfo=vehicleInfoService.findVehicleInfoByVehicleLicense(deviceVehicleVO.getVehicleLicense());
			if(vehicleInfo!=null){
				TripInfo tripInfo=new TripInfo();
	    		tripInfo.setVehicleLicense(vehicleInfo.getVehicleLicense());
	        	tripInfo= tripService.findTripInfoByVehicleLicense(tripInfo);
	        	if(tripInfo==null){
	        		retMap.put("state", -1);
					retMap.put("msg", "行程未开启");
	        	}
				List<PersonInfo> personInfoList=personInfoService.findPersonInfoByVehicleLicense(vehicleInfo.getVehicleLicense());
				List<AlertInfo> alertInfoList=alertInfoService.findAlertInfoByPersonInfoList(personInfoList);
				Map<String, Object> retVehicleInfo=new HashMap<String, Object>();
				retVehicleInfo.put("vehicleLicense", vehicleInfo.getVehicleLicense());
				if(vehicleInfo.getPersonIn()-vehicleInfo.getDeviceCheckPersonCount()>0){
					retVehicleInfo.put("notCheckPersonCount", vehicleInfo.getPersonIn()-vehicleInfo.getDeviceCheckPersonCount());
				}else{
					retVehicleInfo.put("notCheckPersonCount", 0);				
				}
				retVehicleInfo.put("deviceCheckPersonCount", vehicleInfo.getDeviceCheckPersonCount());
				retVehicleInfo.put("personIn", vehicleInfo.getPersonIn());
				retVehicleInfo.put("personOut", vehicleInfo.getPersonOut());
				if(vehicleInfo.getPersonCount()>0){
					retVehicleInfo.put("personCount", vehicleInfo.getPersonCount());
				}else{
					retVehicleInfo.put("personCount", 0);
				}
				retVehicleInfo.put("notScanPersonCount", vehicleInfo.getNotScanPersonCount());
				retVehicleInfo.put("notDisposeAlertCount", alertInfoList.size());
				List<String> organizationNameList=organizationInfoService.findOrganizationInfoByVehicleInfoId(vehicleInfo.getVehicleInfoId());
				retVehicleInfo.put("organizationNameList", organizationNameList);
				retMap.put("state", 1);
				retMap.put("msg", "操作成功");
				retMap.put("vehicleInfo", retVehicleInfo);
			}else{
				retMap.put("state", 0);
				retMap.put("msg", "车辆不存在");
			}
		}catch(Exception e){
			logger.error(e);
			retMap.put("state", 0);
			retMap.put("msg", "系统异常");
		}
		return new ResponseEntity<Map<String,Object>>(retMap, HttpStatus.OK);
	}
	/**
	 * 车辆检查选择
	 * @param deviceVehicleVO
	 * @return
	 */
	@RequestMapping("vehicleCheckChoose")
	@ResponseBody
	public ResponseEntity<Map<String, Object>> vehicleCheckChoose(HttpServletRequest req,@RequestBody DeviceVehicleVO deviceVehicleVO){
		Map<String, Object> retMap=new HashMap<String, Object>();
		try{
			VehicleInfo vehicleInfo=vehicleInfoService.findVehicleInfoByVehicleLicense(deviceVehicleVO.getVehicleLicense());
			if(vehicleInfo!=null){
				if(deviceVehicleVO.getCheckType()==1){
					vehicleInfo.setVehicleType(2);
					vehicleInfoService.updateByPrimaryKeySelective(vehicleInfo);
					vehicleInfoService.deleteCheckPersonCountAndPerson(vehicleInfo);
				}else{
					vehicleInfo.setVehicleType(3);
					vehicleInfoService.updateByPrimaryKeySelective(vehicleInfo);
				}
				retMap.put("state", 1);
				retMap.put("msg", "操作成功");
			}else{
				retMap.put("state", 0);
				retMap.put("msg", "车辆不存在");
			}
		}catch(Exception e){
			e.printStackTrace();
			logger.error(e);
			retMap.put("state", 0);
			retMap.put("msg", "系统异常");
		}
		return new ResponseEntity<Map<String,Object>>(retMap, HttpStatus.OK);
	}
	/**
	 * 车辆放行
	 * @param deviceVehicleVO
	 * @return
	 */
	@RequestMapping("vehiclePass")
	@ResponseBody
	public ResponseEntity<Map<String, Object>> vehiclePass(HttpServletRequest req,@RequestBody DeviceVehicleVO deviceVehicleVO){
		Map<String, Object> retMap=new HashMap<String, Object>();
		try{
			String token=req.getParameter("Token");
			String info=redisService.getString(token);
			ManagerInfo managerInfo=new ManagerInfo();
			managerInfo.setManagerInfoId(JsonUtils.fromJson(info).get("managerInfoId").asLong());
			managerInfo.setManagerName(JsonUtils.fromJson(info).get("managerName").asText());
			managerInfo.setCertificateCode(JsonUtils.fromJson(info).get("certificateCode").asText());
			managerInfo.setOrganizationInfoId(JsonUtils.fromJson(info).get("organizationInfoId").asLong());
			
			VehicleInfo vehicleInfo=vehicleInfoService.findVehicleInfoByVehicleLicense(deviceVehicleVO.getVehicleLicense());
			if(vehicleInfo!=null){
				String vehicleLicense=vehicleInfo.getVehicleLicense();
				Map<String, Object> paramMap=new HashMap<String, Object>();
				paramMap.put("vehicleLicense", vehicleLicense);
//				
//				CheckRecord checkRecord=checkRecordService.findCheckRecordByVehicleLicense(paramMap);
//				List<CheckRecord> checkRecordList=checkRecordService.findCheckRecordListByVehicleLicense(paramMap);
//				OrganizationInfo organizationInfo=organizationInfoService.selectByPrimaryKey(managerInfo.getOrganizationInfoId());
//				//调用省厅回调
//				String checkCallbackUrl=CommonUtil.getPropertiesParam("checkCallbackUrl");
//				String username=CommonUtil.getPropertiesParam("username");
//				String password=CommonUtil.getPropertiesParam("password");
//				String sbid=CommonUtil.getPropertiesParam("sbid");
//				String hpzl=CommonUtil.getPropertiesParam("hpzl");
//				String ldqy=CommonUtil.getPropertiesParam("ldqy");
//				Map<String, Object> paramCheckMap=new HashMap<String, Object>();
//				paramCheckMap.put("username", username);
//				paramCheckMap.put("password", password);
//				paramCheckMap.put("sbid", sbid);
//				paramCheckMap.put("cphm", deviceVehicleVO.getVehicleLicense());
//				paramCheckMap.put("hpzl", hpzl);
//				paramCheckMap.put("checkId", managerInfo.getCertificateCode());
//				paramCheckMap.put("checkName", managerInfo.getManagerName());
//				paramCheckMap.put("name", organizationInfo.getOrganizationName());
//				paramCheckMap.put("ldqy", ldqy);
//				List<Map<String, Object>> zbclList=new ArrayList<Map<String, Object>>();
//				if(checkRecord!=null){
//					String zbclJson=checkRecord.getCheckReturnComment();
//					Iterator<JsonNode> zbclJsonList=JsonUtils.fromJson(zbclJson).get("data").get("zbclList").iterator();
//					if(zbclJsonList.hasNext()){
//						while(zbclJsonList.hasNext()){
//							Map<String, Object> zbclMap=new HashMap<String, Object>();
//							JsonNode zbcl=zbclJsonList.next();
//							zbclMap.put("bklxfs", ObjectUtils.isEmpty(zbcl.get("bklxfs"))?null:zbcl.get("bklxfs").asText());
//							zbclMap.put("pverid", ObjectUtils.isEmpty(zbcl.get("pverid"))?null:zbcl.get("pverid").asText());
//							zbclMap.put("bklxr", ObjectUtils.isEmpty(zbcl.get("bklxr"))?null:zbcl.get("bklxr").asText());
//							zbclMap.put("clfs", ObjectUtils.isEmpty(zbcl.get("clfs"))?null:zbcl.get("clfs").asText());
//							zbclMap.put("aqms", ObjectUtils.isEmpty(zbcl.get("aqms"))?null:zbcl.get("aqms").asText());
//							zbclMap.put("pfirstid", ObjectUtils.isEmpty(zbcl.get("pfirstid"))?null:zbcl.get("pfirstid").asText());
//							zbclMap.put("rwid", ObjectUtils.isEmpty(zbcl.get("rwid"))?null:zbcl.get("rwid").asText());
//							zbclMap.put("rwmc", ObjectUtils.isEmpty(zbcl.get("rwmc"))?null:zbcl.get("rwmc").asText());
//							zbclMap.put("cllb", ObjectUtils.isEmpty(zbcl.get("cllb"))?null:zbcl.get("cllb").asText());
//							zbclMap.put("clsbdm", ObjectUtils.isEmpty(zbcl.get("clsbdm"))?null:zbcl.get("clsbdm").asText());
//							zbclMap.put("clys", ObjectUtils.isEmpty(zbcl.get("clys"))?null:zbcl.get("clys").asText());
//							zbclMap.put("fdjh", ObjectUtils.isEmpty(zbcl.get("fdjh"))?null:zbcl.get("fdjh").asText());
//							zbclMap.put("czsfzh", ObjectUtils.isEmpty(zbcl.get("czsfzh"))?null:zbcl.get("czsfzh").asText());
//							zbclMap.put("czxm", ObjectUtils.isEmpty(zbcl.get("czxm"))?null:zbcl.get("czxm").asText());
//							zbclMap.put("cphm", ObjectUtils.isEmpty(zbcl.get("cphm"))?null:zbcl.get("cphm").asText());
//							zbclList.add(zbclMap);
//						}
//					}
//				}
//				paramCheckMap.put("zbclList", zbclList);
//				List<Map<String, Object>> ryxxList=new ArrayList<Map<String, Object>>();
//				List<Map<String, Object>> zbryList=new ArrayList<Map<String, Object>>();
//				for(CheckRecord personCheckRecord:checkRecordList){
//					String certificateCode=personCheckRecord.getCheckCode();
//					PersonInfo personInfo=personInfoService.findPersonInfoByCertificateCode(certificateCode);
//					Map<String, Object> ryxxMap=new HashMap<String, Object>();
//					ryxxMap.put("sfzzz", personInfo.getAddress());
//					ryxxMap.put("xb", personInfo.getGender());
//					ryxxMap.put("checkevent_id", null);//事件id
//					ryxxMap.put("rylb", null);//人员类别
//					ryxxMap.put("check_user", managerInfo.getManagerName());//核查人
//					ryxxMap.put("equipt_id", sbid);
//					ryxxMap.put("dept_name", organizationInfo.getOrganizationName()); 
//					ryxxMap.put("pcsj", personInfo.getCreateDate().getTime()); 
//					ryxxMap.put("sfzh", personInfo.getCertificateCode()); 
//					ryxxMap.put("xm", personInfo.getName()); 
//					if(!StringUtils.isEmpty(personInfo.getCertificateFaceUrl())){
//						FTPClient ftpClient =null;
//						InputStream is=null;
//						try{
//							ftpClient=FtpUtil.connect(CommonUtil.getPropertiesParam("ftp_address"), Integer.parseInt(CommonUtil.getPropertiesParam("ftp_port")), CommonUtil.getPropertiesParam("ftp_user"), CommonUtil.getPropertiesParam("ftp_password"));
//							int changeWorkingLength = personInfo.getCertificateFaceUrl().split("/").length-1;
//							if(changeWorkingLength>0){
//								FtpUtil.changeWorkingDirectory(personInfo.getCertificateFaceUrl().substring(0, personInfo.getCertificateFaceUrl().lastIndexOf("/")), ftpClient);
//								is=FtpUtil.retrieveFileStream(personInfo.getCertificateFaceUrl().substring(personInfo.getCertificateFaceUrl().lastIndexOf("/")+1,personInfo.getCertificateFaceUrl().length()), ftpClient);
//							}else{
//								is=FtpUtil.retrieveFileStream(personInfo.getCertificateFaceUrl(), ftpClient);
//							}
//							ByteArrayOutputStream output = new ByteArrayOutputStream();
//						    byte[] buffer = new byte[4096];
//						    int n = 0;
//						    while (-1 != (n = is.read(buffer))) {
//						        output.write(buffer, 0, n);
//						    }
//						    ryxxMap.put("xp", output.toByteArray());
//						}catch (Exception e) {
//							logger.error("文件读取失败", e);
//							ryxxMap.put("mz", null);
//						}finally {
//							try {
//								FtpUtil.closeConnection(ftpClient);
//							} catch (Exception e) {
//								ftpClient=null;
//							}
//							try {
//								if(is!=null){
//									is.close();
//								}
//							} catch (Exception e) {
//								is=null;
//							}
//						}
//					}else{
//						ryxxMap.put("xp", null);
//					}
//					ryxxMap.put("mz", personInfo.getNationality()); 
//					ryxxList.add(ryxxMap);
//					String zbryJson=personCheckRecord.getCheckReturnComment();
//					Iterator<JsonNode> zbryJsonList=JsonUtils.fromJson(zbryJson).get("data").get("zbryList").iterator();
//					if(zbryJsonList.hasNext()){
//						while(zbryJsonList.hasNext()){
//							Map<String, Object> zbryMap=new HashMap<String, Object>();
//							JsonNode zbry=zbryJsonList.next();
//							zbryMap.put("bklxfs", ObjectUtils.isEmpty(zbry.get("bklxfs"))?null:zbry.get("bklxfs").asText());
//							zbryMap.put("sfzh", ObjectUtils.isEmpty(zbry.get("sfzh"))?null:zbry.get("sfzh").asText());
//							zbryMap.put("pverid", ObjectUtils.isEmpty(zbry.get("pverid"))?null:zbry.get("pverid").asText());
//							zbryMap.put("bklxr", ObjectUtils.isEmpty(zbry.get("bklxr"))?null:zbry.get("bklxr").asText());
//							zbryMap.put("rylb", ObjectUtils.isEmpty(zbry.get("rylb"))?null:zbry.get("rylb").asText());
//							zbryMap.put("clfs", ObjectUtils.isEmpty(zbry.get("clfs"))?null:zbry.get("clfs").asText());
//							zbryMap.put("aqms", ObjectUtils.isEmpty(zbry.get("aqms"))?null:zbry.get("aqms").asText());
//							zbryMap.put("pfirstid", ObjectUtils.isEmpty(zbry.get("pfirstid"))?null:zbry.get("pfirstid").asText());
//							zbryMap.put("rwid", ObjectUtils.isEmpty(zbry.get("rwid"))?null:zbry.get("rwid").asText());
//							zbryMap.put("rwmc", ObjectUtils.isEmpty(zbry.get("rwmc"))?null:zbry.get("rwmc").asText());
//							zbryMap.put("xm", personInfo.getName());
//							zbryList.add(zbryMap);
//						}
//					}
//				}
//				String checkJson=JsonUtils.toJson(paramCheckMap);
//				logger.trace("省厅回调请求Json："+checkJson);
//				Map<String, String> postParamCheckMap=new HashMap<String, String>();
//				postParamCheckMap.put("jsonObj", checkJson);
//				String dataStr=HttpClientUtil.doPost(checkCallbackUrl, postParamCheckMap);
//				logger.trace("省厅回调返回："+dataStr);
//				if(!StringUtils.isEmpty(dataStr)){
//					String code=JsonUtils.fromJson(dataStr).get("code").asText();
//					if(!"000".equals(code)){
//						logger.error("省厅回调错误返回："+dataStr);
//					}
//				}
				checkRecordService.deleteByVehicleLicense(paramMap);
				redisService.removeString(vehicleInfo.getVehicleLicense()+"_eventId");
				if(vehicleInfo.getVehicleType()==2){
					List<PersonInfo> personInfoList=personInfoService.findPersonInfoByVehicleLicense(vehicleInfo.getVehicleLicense());
					vehicleInfo.setPersonCount(personInfoList.size());
					vehicleInfo.setVehicleType(1);
				}else if(vehicleInfo.getVehicleType()==3){
					if(deviceVehicleVO.getPersonCount()!=null){
						vehicleInfo.setPersonCount(deviceVehicleVO.getPersonCount());
					}
					vehicleInfo.setVehicleType(1);
				}else if(vehicleInfo.getVehicleType()==0){
					vehicleInfoService.deleteVehicleInfo(vehicleInfo);
					retMap.put("state", 1);
					retMap.put("msg", "操作成功");
					return new ResponseEntity<Map<String,Object>>(retMap, HttpStatus.OK);
				}
				vehicleInfo.setDeviceCheckPersonCount(0);
				vehicleInfo.setCheckPersonCount(0);
				vehicleInfo.setPersonIn(0);
				vehicleInfo.setPersonOut(0);
				vehicleInfoService.updateByPrimaryKeySelective(vehicleInfo);
				Organization_vehicle organization_vehicle=new Organization_vehicle();
				organization_vehicle.setOrganizationInfoId(managerInfo.getOrganizationInfoId());
				organization_vehicle.setVehicleInfoId(vehicleInfo.getVehicleInfoId());
				organization_vehicleService.insertSelective(organization_vehicle);
				
				redisService.setString(vehicleInfo.getVehicleLicense()+"_command","1");
				retMap.put("state", 1);
				retMap.put("msg", "操作成功");
			}else{
				retMap.put("state", 0);
				retMap.put("msg", "车辆不存在");
			}
		}catch(Exception e){
			e.printStackTrace();
			logger.error(e);
			retMap.put("state", 0);
			retMap.put("msg", "系统异常");
		}
		return new ResponseEntity<Map<String,Object>>(retMap, HttpStatus.OK);
	}
	
	/**
	 * 车辆开始检查
	 * @param deviceVehicleVO
	 * @return
	 */
	@RequestMapping("vehicleCheckStart")
	@ResponseBody
	public ResponseEntity<Map<String, Object>> vehicleCheckStart(HttpServletRequest req,@RequestBody DeviceVehicleVO deviceVehicleVO){
		Map<String, Object> retMap=new HashMap<String, Object>();
		try{
			String token=req.getParameter("Token");
			String info=redisService.getString(token);
			ManagerInfo managerInfo=new ManagerInfo();
			managerInfo.setManagerInfoId(JsonUtils.fromJson(info).get("managerInfoId").asLong());
			managerInfo.setManagerName(JsonUtils.fromJson(info).get("managerName").asText());
			managerInfo.setCertificateCode(JsonUtils.fromJson(info).get("certificateCode").asText());
			managerInfo.setOrganizationInfoId(JsonUtils.fromJson(info).get("organizationInfoId").asLong());
			VehicleInfo vehicleInfo=vehicleInfoService.findVehicleInfoByVehicleLicense(deviceVehicleVO.getVehicleLicense());
			if(vehicleInfo==null){
				vehicleInfo=new VehicleInfo();
				vehicleInfo.setVehicleLicense(deviceVehicleVO.getVehicleLicense());
				vehicleInfo.setVehicleType(0);
				vehicleInfo.setState(1);
				vehicleInfo.setCreateDate(new Date());
				vehicleInfoService.insertSelective(vehicleInfo);
				retMap.put("state", 2);
				retMap.put("msg", "无设备车辆");
			}else if(vehicleInfo.getVehicleType()==0){
				retMap.put("state", 2);
				retMap.put("msg", "无设备车辆");
			}else{
				retMap.put("state", 1);
				retMap.put("msg", "有设备车辆");
			}
			OrganizationInfo organizationInfo=organizationInfoService.selectByPrimaryKey(managerInfo.getOrganizationInfoId());
			String eventId=CommonUtil.getUUID();
			Map<String, String> paramCheckMap=new HashMap<String, String>();
			String sdkCheckVehicleUrl=CommonUtil.getPropertiesParam("sdkCheckVehicleUrl");
			String companyId=CommonUtil.getPropertiesParam("companyId");
			String hpzl=CommonUtil.getPropertiesParam("hpzl");
			paramCheckMap.put("companyId", companyId);
			paramCheckMap.put("eventId", eventId);
			paramCheckMap.put("recordId", CommonUtil.getUUID());
			CheckCarInfo checkCarInfo=new CheckCarInfo();
			checkCarInfo.setCphm(deviceVehicleVO.getVehicleLicense());
			checkCarInfo.setGpsX(organizationInfo.getLatitude());
			checkCarInfo.setGpsY(organizationInfo.getLongitude());
			checkCarInfo.setHpzl(hpzl);
			paramCheckMap.put("checkCarInfo", JsonUtils.toJson(checkCarInfo));
			logger.trace("省厅sdk核查车辆请求："+JsonUtils.toJson(checkCarInfo));
			String dataStr=HttpClientUtil.doPost(sdkCheckVehicleUrl, paramCheckMap);
			logger.trace("省厅sdk核查车辆返回："+dataStr);
			String code=JsonUtils.fromJson(dataStr).get("code").asText();
			if("000".equals(code)){
//				Iterator<JsonNode> zbclList=JsonUtils.fromJson(dataStr).get("data").iterator();
//				if(zbclList.hasNext()){
//					retMap.put("state", 3);
//					retMap.put("msg", "中标车辆");
//					List<Map<String, Object>> vehicleAlertList=new ArrayList<Map<String, Object>>();
//					while(zbclList.hasNext()){
//						JsonNode zbcl=zbclList.next();
//						Map<String, Object> vehicleAlertMap=new HashMap<String, Object>();
//						vehicleAlertMap.put("alertVehicleType", ObjectUtils.isEmpty(zbcl.get("cllb"))?null:zbcl.get("rcllbmc").asText());
//						vehicleAlertMap.put("alertHandleFun", ObjectUtils.isEmpty(zbcl.get("clfs"))?null:zbcl.get("clfs").asText());
//						vehicleAlertMap.put("alertComment", ObjectUtils.isEmpty(zbcl.get("aqms"))?null:zbcl.get("aqms").asText());
//						vehicleAlertMap.put("alertLinkName", ObjectUtils.isEmpty(zbcl.get("bklxr"))?null:zbcl.get("bklxr").asText());
//						vehicleAlertMap.put("alertLinkPhone", ObjectUtils.isEmpty(zbcl.get("bklxfs"))?null:zbcl.get("bklxfs").asText());
//						vehicleAlertMap.put("vehicleOwnerCertificateCode", ObjectUtils.isEmpty(zbcl.get("czsfzh"))?null:zbcl.get("czsfzh").asText());
//						vehicleAlertMap.put("vehicleOwnerName", ObjectUtils.isEmpty(zbcl.get("czxm"))?null:zbcl.get("czxm").asText());
//						vehicleAlertMap.put("vehicleColor", ObjectUtils.isEmpty(zbcl.get("clys"))?null:zbcl.get("clys").asText());
//						vehicleAlertMap.put("vehicleNumber", ObjectUtils.isEmpty(zbcl.get("fdjh"))?null:zbcl.get("fdjh").asText());
//						vehicleAlertList.add(vehicleAlertMap);
//					}
//					retMap.put("vehicleAlertList", vehicleAlertList);
//				}
			}else{
				logger.trace("核查车辆失败:"+JsonUtils.fromJson(dataStr).get("msg").asText());
			}
			CheckRecord checkRecord=new CheckRecord();
			checkRecord.setCheckType(1);
			checkRecord.setVehicleLicense(vehicleInfo.getVehicleLicense());
			checkRecord.setCheckReturnComment(dataStr);
			checkRecord.setCreateDate(new Date());
			checkRecord.setState(1);
			checkRecordService.insertSelective(checkRecord);
			redisService.setString(deviceVehicleVO.getVehicleLicense()+"_eventId", eventId);
//			Map<String, Object> paramMap=new HashMap<String, Object>();
//			paramMap.put("vehicleLicense", deviceVehicleVO.getVehicleLicense());
//			CheckRecord checkRecord=checkRecordService.findCheckRecordByVehicleLicense(paramMap);
//			if(checkRecord==null){
//				//调用省厅核查
//				String checkUrl=CommonUtil.getPropertiesParam("checkUrl");
//				String username=CommonUtil.getPropertiesParam("username");
//				String password=CommonUtil.getPropertiesParam("password");
//				String sbid=CommonUtil.getPropertiesParam("sbid");
//				String hpzl=CommonUtil.getPropertiesParam("hpzl");
//				String ldqy=CommonUtil.getPropertiesParam("ldqy");
//				Map<String, String> paramCheckMap=new HashMap<String, String>();
//				paramCheckMap.put("username", username);
//				paramCheckMap.put("password", password);
//				paramCheckMap.put("sbid", sbid);
//				paramCheckMap.put("cphm", deviceVehicleVO.getVehicleLicense());
//				paramCheckMap.put("checkId", managerInfo.getCertificateCode());
//				paramCheckMap.put("checkName", managerInfo.getManagerName());
//				paramCheckMap.put("name", organizationInfo.getOrganizationName());
//				paramCheckMap.put("hpzl", hpzl);
//				paramCheckMap.put("ldqy", ldqy);
//				String checkJson=JsonUtils.toJson(paramCheckMap);
//				Map<String, String> postParamCheckMap=new HashMap<String, String>();
//				postParamCheckMap.put("jsonObj", checkJson);
//				String dataStr=HttpClientUtil.doPost(checkUrl, postParamCheckMap);
//				logger.trace("省厅核查返回："+dataStr);
//				if(!StringUtils.isEmpty(dataStr)){
//					String code=JsonUtils.fromJson(dataStr).get("code").asText();
//					if("000".equals(code)){
//						checkRecord=new CheckRecord();
//						checkRecord.setCheckType(1);
//						checkRecord.setVehicleLicense(vehicleInfo.getVehicleLicense());
//						checkRecord.setCheckReturnComment(dataStr);
//						checkRecord.setCreateDate(new Date());
//						checkRecord.setState(1);
//						checkRecordService.insertSelective(checkRecord);
//						//判断车辆是否中标
//						Iterator<JsonNode> zbclList=JsonUtils.fromJson(dataStr).get("data").get("zbclList").iterator();
//						if(zbclList.hasNext()){
//							retMap.put("state", 3);
//							retMap.put("msg", "中标车辆");
//							List<Map<String, Object>> vehicleAlertList=new ArrayList<Map<String, Object>>();
//							while(zbclList.hasNext()){
//								JsonNode zbcl=zbclList.next();
//								Map<String, Object> vehicleAlertMap=new HashMap<String, Object>();
//								vehicleAlertMap.put("alertVehicleType", ObjectUtils.isEmpty(zbcl.get("cllb"))?null:zbcl.get("rcllbmc").asText());
//								vehicleAlertMap.put("alertHandleFun", ObjectUtils.isEmpty(zbcl.get("clfs"))?null:zbcl.get("clfs").asText());
//								vehicleAlertMap.put("alertComment", ObjectUtils.isEmpty(zbcl.get("aqms"))?null:zbcl.get("aqms").asText());
//								vehicleAlertMap.put("alertLinkName", ObjectUtils.isEmpty(zbcl.get("bklxr"))?null:zbcl.get("bklxr").asText());
//								vehicleAlertMap.put("alertLinkPhone", ObjectUtils.isEmpty(zbcl.get("bklxfs"))?null:zbcl.get("bklxfs").asText());
//								vehicleAlertMap.put("vehicleOwnerCertificateCode", ObjectUtils.isEmpty(zbcl.get("czsfzh"))?null:zbcl.get("czsfzh").asText());
//								vehicleAlertMap.put("vehicleOwnerName", ObjectUtils.isEmpty(zbcl.get("czxm"))?null:zbcl.get("czxm").asText());
//								vehicleAlertMap.put("vehicleColor", ObjectUtils.isEmpty(zbcl.get("clys"))?null:zbcl.get("clys").asText());
//								vehicleAlertMap.put("vehicleNumber", ObjectUtils.isEmpty(zbcl.get("fdjh"))?null:zbcl.get("fdjh").asText());
//								vehicleAlertList.add(vehicleAlertMap);
//							}
//							retMap.put("vehicleAlertList", vehicleAlertList);
//						}
//					}else{
//						logger.trace("核查车辆失败:"+JsonUtils.fromJson(dataStr).get("msg").asText());
//					}
//				}
//			}
		}catch(Exception e){
			logger.error(e);
			retMap.put("state", 0);
			retMap.put("msg", "系统异常");
		}
		return new ResponseEntity<Map<String,Object>>(retMap, HttpStatus.OK);
	}
	/**
	 * 车辆行程结束
	 * @param deviceVehicleVO
	 * @return
	 *//*
	@RequestMapping("vehicleCheckStop")
	@ResponseBody
	public ResponseEntity<Map<String, Object>> vehicleCheckStop(@RequestBody DeviceVehicleVO deviceVehicleVO){
		Map<String, Object> retMap=new HashMap<String, Object>();
		try{
			VehicleInfo vehicleInfo=vehicleInfoService.findVehicleInfoByVehicleLicense(deviceVehicleVO.getVehicleLicense());
			if(vehicleInfo!=null){
				vehicleInfoService.deleteVehicleInfoAndPerson(vehicleInfo);
				retMap.put("state", 1);
				retMap.put("msg", "操作成功");
			}else{
				retMap.put("state", 0);
				retMap.put("msg", "车辆不存在");
			}
		}catch(Exception e){
			logger.error(e);
			retMap.put("state", 0);
			retMap.put("msg", "系统异常");
		}
		return new ResponseEntity<Map<String,Object>>(retMap, HttpStatus.OK);
	}*/
}
